/* Magic Mirror
 * Node Helper: MMM-ReceiveData
 *
 * By coffee_java
 * MIT Licensed.
 */

var NodeHelper = require("node_helper");
var exec = require('child_process');

module.exports = NodeHelper.create({
	// Subclass start method.
	start: function() {
		console.log("Starting MMM-DH11 helper: " + this.name);
	},
	readData:function(payload){
		   var self = this;
		   this.config = payload
		   
		   // execute external DHT Script
		   console.log("命令 "+__dirname+"/dht_var " + this.config.sensorPIN)
		   exec.exec("sudo "+__dirname+"/dht_var " + this.config.sensorPIN, (error, stdout) => {
			   if (error) {
					console.log("exec error: "+error);
					return;
				  }
				  var arr = stdout.split(",");
				  
				  console.log("temperature: " + arr[1]);
				  console.log("humidity: " + arr[0]);
				  // Send Temperatur
				  self.sendSocketNotification("UPDATE_DATA",{
						temperature: arr[1],
						humidity: arr[0]
				  });
		    });
		 
	},
	// Subclass socketNotificationReceived received.
	socketNotificationReceived: function(notification, payload) {
		console.log("MMM-DH11 helper:receive:"+notification)
		if (notification === 'GET_DATA') {
			this.readData(payload);
		}
		 
	}
});